package viper.GUI;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle;

import viper.main.StateManager;
import viper.main.ViperHeader;

/**
 *
 * @author Kevin Sin
 * 
 * Revision number: 1
 * Date created: 1/17/10
 * Date of Last Back-up on SVN: none
 * 
 * <p>This JPanel is used right after the user selects who he wants to call.  
 * Lets the user select the diversity code and the relays to use. 
 */
public class CallSenderSetupPanel extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form LoggedInJPanel */
    public CallSenderSetupPanel() {
        initComponents();
    }
    

    private JButton jButton1; //Log out button
    private JButton jButton2; //Call button
    private JButton jButton3; //Back button
    private JLabel jLabel1; //Directions
    private JLabel jLabel2; //Directions
    private JPanel jPanel1; //Directions
    private JRadioButton jRadioButton1; //(2,3,2) code radio button
    private JRadioButton jRadioButton2; //(3,6,4) code radio button
    private JRadioButton jRadioButton3; //(1,0,1) code radio button
    private JRadioButton jRadioButton4; //(1,1,1) code radio button
    private ButtonGroup buttonGroup; //For the diversity code buttons
    private JScrollPane jScrollPane1; //For displaying possible relays
    
    private ArrayList<JCheckBox> checkBoxArrayList = 
    	new ArrayList<JCheckBox>(); //checkbox ArrayList for relays to choose.
    private ArrayList<Object[]> relaysSelected = 
    	new ArrayList<Object[]>(); //represents the relays the user has selected 
    private ArrayList<Object[]> possibleRelays = 
    	new ArrayList<Object[]>(); //represents the possible relays to use

    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {

        jButton1 = new JButton();
        jLabel1 = new JLabel();
        jButton2 = new JButton();
        jButton3 = new JButton();
        jLabel2 = new JLabel();
        jRadioButton1 = new JRadioButton();
        jRadioButton2 = new JRadioButton();
        jRadioButton3 = new JRadioButton();
        jRadioButton4 = new JRadioButton();
        jScrollPane1 = new JScrollPane();
        
        jPanel1 = new JPanel(new GridLayout(0, 1));
        
        buttonGroup = new ButtonGroup();

        jButton1.setText("Log out");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Call >");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("< Back");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("Choose the diversity code:");
        jLabel2.setText("and your relays:");

        jRadioButton1.setText("(2, 3, 2)");
        jRadioButton2.setText("(3, 6, 4)");
        jRadioButton3.setText("(1, 0, 1)");
        jRadioButton4.setText("(1, 1, 1)");
        jRadioButton3.setSelected(true);
        
        buttonGroup.add(jRadioButton3);
        buttonGroup.add(jRadioButton4);
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2); 

        jScrollPane1.setViewportView(jPanel1);

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
        		layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        				.addGap(20, 20, 20)
        				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        						.addGroup(layout.createSequentialGroup()
        								.addComponent(jRadioButton3)
        								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 174, Short.MAX_VALUE)
        								.addComponent(jRadioButton4)
        								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 174, Short.MAX_VALUE)
        								.addComponent(jRadioButton1)
        								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 174, Short.MAX_VALUE)
        								.addComponent(jRadioButton2)
        								.addGap(128, 128, 128))
        								.addGroup(layout.createSequentialGroup()
        										.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        												.addComponent(jLabel1)
        												.addComponent(jLabel2))
        												.addContainerGap(303, Short.MAX_VALUE))
        												.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        														.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
        																.addComponent(jScrollPane1, GroupLayout.Alignment.LEADING)
        																.addGroup(layout.createSequentialGroup()
        																		.addGap(284, 284, 284)
        																		.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
        																				.addGroup(layout.createSequentialGroup()
        																						.addComponent(jButton3)
        																						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        																						.addComponent(jButton2))
        																						.addComponent(jButton1))))
        																						.addGap(20, 20, 20))))
        );
        layout.setVerticalGroup(
        		layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        				.addContainerGap()
        				.addComponent(jButton1)
        				.addGap(8, 8, 8)
        				.addComponent(jLabel1)
        				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        						.addComponent(jRadioButton3)
        						.addComponent(jRadioButton4)
        						.addComponent(jRadioButton1)
        						.addComponent(jRadioButton2))
        						.addGap(16, 16, 16)
        						.addComponent(jLabel2)
        						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        						.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
        						.addGap(29, 29, 29)
        						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        								.addComponent(jButton2)
        								.addComponent(jButton3))
        								.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }

    /**
     * @param evt
     * Logout button pressed
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        StateManager.INSTANCE.logout();
    }                                        

    /**
     * @param evt
     * Call button pressed
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {  
    	if(jRadioButton1.isSelected()) //(2,3,2) code selected, check if 3 relays selected
    	{
    		if(countChecks()==3)
    		{
    			StateManager.INSTANCE.setRelays(relaysSelected);
    			StateManager.INSTANCE.setDiversityCode(ViperHeader.CODE_232);
    			StateManager.INSTANCE.call();
    		}
    		else
    			JOptionPane.showMessageDialog(this, "Please select 3 relays!");
    	}
    	else if(jRadioButton3.isSelected()) //(1,0,1) code selected, check 0 relays selected
    	{
    		if(countChecks()==0)
    		{
    			StateManager.INSTANCE.setRelays(new ArrayList<Object[]>());
    			StateManager.INSTANCE.setDiversityCode(ViperHeader.CODE_101);
    			StateManager.INSTANCE.call();
    		}
    		else
    			JOptionPane.showMessageDialog(this, "Please select 0 relays!");
    	}
    	else if(jRadioButton4.isSelected())
    	{
    		if(countChecks()==1)
    		{
    			StateManager.INSTANCE.setRelays(relaysSelected);
    			StateManager.INSTANCE.setDiversityCode(ViperHeader.CODE_111);
    			StateManager.INSTANCE.call();
    		}
    		else
    			JOptionPane.showMessageDialog(this, "Please select 1 relay!");
    	}
    	else //(3, 6, 4) code selected, check if 6 relays selected
    	{
    		if(countChecks()==6)
    		{
    			StateManager.INSTANCE.setRelays(relaysSelected);
    			StateManager.INSTANCE.setDiversityCode(ViperHeader.CODE_364);
    			StateManager.INSTANCE.call();
    		}
    		else
    			JOptionPane.showMessageDialog(this, "Please select 6 relays!");
    	}
        
    }                                 
    
    /**
     * @return number of checkboxes checked
     */
    private int countChecks() {
    	relaysSelected.clear();
    	int count = 0;
    	int i = 0;
    	for(JCheckBox jcb: checkBoxArrayList)
    	{
    		if(jcb.isSelected())
    		{
    			count++;
    			relaysSelected.add(possibleRelays.get(i));
    		}
    		i++;
    	}
    	return count;
    }

    /**
     * @param evt
     * Back button pressed
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    	ViperGUI.INSTANCE.changeCard(ViperGUI.LOGGED_IN_CARD);
    }
    
    /**
     * Updates possibleRelays.  Make sure callee is not included.
     */
    private void updatePossibleRelays() {
    	possibleRelays = StateManager.INSTANCE.getPeers();
    	for(Object[] peerArray : possibleRelays)
    	{
    		if(((String)(StateManager.INSTANCE.getCalleeArray()[0])).equals((String)(peerArray[0])))
    		{
    			possibleRelays.remove(peerArray);
    			break;
    		}
    	}
    }

    /**
     * Update relays and display relays
     */
    public void update(){
    	updatePossibleRelays();
    	jPanel1.removeAll();
    	checkBoxArrayList.clear();
    	for(Object[] relayArray : possibleRelays)
    	{   		
    		JCheckBox jcb = new JCheckBox();
    		jcb.setText(relayArray[1] + "---" + relayArray[0] + "---" + relayArray[2]);
    		jPanel1.add(jcb);
    		checkBoxArrayList.add(jcb);	
    	}
    }
}

